<!--
 Copyright (c) 2016,2017,2018,2019,2020,2021,2022 Klaus Landsdorf (http://node-red.plus/)
 All rights reserved.
 node-red-contrib-modbus - The BSD 3-Clause License

 @author <a href="mailto:klaus.landsdorf@bianco-royal.de">Klaus Landsdorf</a> (Bianco Royal)
-->
<script type="text/javascript">
    RED.nodes.registerType('modbus-queue-info', {
        category: 'modbus',
        color: '#E9967A',
        defaults: {
            name: { value: '' },
            topic: { value: '' },
            unitid: { value: 1, validate: RED.validators.number() },
            queueReadIntervalTime: { value: 1000, validate: RED.validators.number() },
            lowLowLevel: { value: 25, validate: RED.validators.number() },
            lowLevel: { value: 75, validate: RED.validators.number() },
            highLevel: { value: 150, validate: RED.validators.number() },
            highHighLevel: { value: 300, validate: RED.validators.number() },
            server: { type: 'modbus-client', required: true },
            errorOnHighLevel: { value: false },
            showStatusActivities: { value: true },
            updateOnAllQueueChanges: { value: false },
            updateOnAllUnitQueues: { value: false }
        },
        inputs: 1,
        outputs: 1,
        align: "right",
        icon: 'modbus.png',
        paletteLabel: 'Modbus-Queue-Info',
        label: function () {
            return this.name || 'Modbus Queue Info'
        },
      oneditprepare: function () {

        let tabs = RED.tabs.create({
          id: "node-input-modbus-tabs",
          onchange: function (tab) {
            $("#node-input-tabs-content").children().hide()
            $("#" + tab.id).show()
          }
        })

        tabs.addTab({
          id: "modbus-settings-tab",
          label: this._("modbus-contrib.tabs-label.settings")
        })

        tabs.addTab({
          id: "modbus-options-tab",
          label: this._("modbus-contrib.tabs-label.options")
        })
      }
    })
</script>


<script type="text/x-red" data-template-name="modbus-queue-info">
    <div class="form-row">
        <ul style="background: #fff; min-width: 600px; margin-bottom: 20px;" id="node-input-modbus-tabs"></ul>
    </div>
    <div id="node-input-tabs-content" style="min-height: 170px;">
        <div id="modbus-settings-tab" style="display:none">
            <div class="form-row">
                <label for="node-input-name"><i class="icon-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
                <input type="text" id="node-input-name" placeholder="Name">
            </div>
            <div class="form-row">
                <label for="node-input-topic"><i class="icon-tag"></i> <span data-i18n="node-red:common.label.topic"></span></label>
                <input type="text" id="node-input-topic" placeholder="Topic">
            </div>
            <div class="form-row">
                <label for="node-input-unitid"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.unitId"></span></label>
                <input type="text" id="node-input-unitid" placeholder="1">
            </div>
            <div class="form-row">
                <label for="node-input-queueReadIntervalTime"><i class="icon-clock"></i> <span data-i18n="modbus-contrib.label.queueReadIntervalTime"></span></label>
                <input type="text" id="node-input-queueReadIntervalTime" placeholder="1000" style="width:140px;"> msec.
            </div>
            <hr>
            <div class="form-row">
                <label for="node-input-lowLowLevel"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.lowLowLevel"></span></label>
                <input type="text" id="node-input-lowLowLevel" placeholder="25">
            </div>
            <div class="form-row">
                <label for="node-input-lowLevel"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.lowLevel"></span></label>
                <input type="text" id="node-input-lowLevel" placeholder="75">
            </div>
            <div class="form-row">
                <label for="node-input-highLevel"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.highLevel"></span></label>
                <input type="text" id="node-input-highLevel" placeholder="150">
            </div>
            <div class="form-row">
                <label for="node-input-highHighLevel"><i class="icon-bookmark"></i> <span data-i18n="modbus-contrib.label.highHighLevel"></span></label>
                <input type="text" id="node-input-highHighLevel" placeholder="300">
            </div>
            <div class="form-row">
                <label for="node-input-server"><i class="icon-globe"></i> <span data-i18n="modbus-contrib.label.server"></span></label>
                <input type="text" id="node-input-server">
            </div>
        </div>
        <div id="modbus-options-tab" style="display:none">
            <div class="form-row">
                <label style="min-width:190px" for="node-input-updateOnAllQueueChanges"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.updateOnAllQueueChanges"></span></label>
                <input type="checkbox" id="node-input-updateOnAllQueueChanges" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-updateOnAllUnitQueues"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.updateOnAllUnitQueues"></span></label>
                <input type="checkbox" id="node-input-updateOnAllUnitQueues" style="max-width:30px">
            </div>
            <hr>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-errorOnHighLevel"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.errorOnHighLevel"></span></label>
                <input type="checkbox" id="node-input-errorOnHighLevel" style="max-width:30px">
            </div>
            <div class="form-row">
                <label style="min-width:190px" for="node-input-showStatusActivities"><i class="fa fa-th"></i> <span
                data-i18n="modbus-contrib.label.showActivities"></span></label>
                <input type="checkbox" id="node-input-showStatusActivities" style="max-width:30px">
            </div>
        </div>
    </div>
</script>


<script type="text/x-red" data-help-name="modbus-queue-info">
    <p><strong>
        A queue is set per unit - setup the unit-id to get the right information.
    </strong></p>

    <p>Modbus TCP/Serial queue information node.</p>

     <p>Unit-Id (0..255 tcp | 0..247 serial)</p>
     <p>Unit-Id 0 is for broadcasting Modbus messages.</p>

     <p>Use inject of <pre>msg.resetQueue = true</pre> to
      reset the queue.</p>

      <p>Reset on high level to connect with the catch node</p>
      <p><pre>
        [{"id":"430f76bf.9de2d8","type":"function","z":"b245d3e4.b52de","name":"reset on High",
        "func":"if(\"high level reached\" === msg.state) {\n    msg.payload.resetQueue = true;\n    return msg;\n}\n",
        "outputs":1,"noerr":0,"x":410,"y":140,"wires":[[]]}]
      </pre></p>

      <p>Reset on high high level to connect with the catch node</p>
      <p>
          <pre>
            [{"id":"fc74491.2ddc9b8","type":"function","z":"b245d3e4.b52de","name":"reset on HighHigh",
            "func":"if(\"high high level reached\" === msg.state) {\n    msg.payload.resetQueue = true;\n    return msg;\n}\n",
            "outputs":1,"noerr":0,"x":430,"y":180,"wires":[[]]}]
          </pre>
      </p>

     <p>
        An update of all unit queues can be watched by checking the option "All Queues (0-255)".
        With that option it sends out messages per queue event with the queue number and info.
        Use just a single queue info node per client configuration if you activate that option!
        A reset input will reset all queues (0-255) in that mode.
     </p>

     <p>"Error on high level" with that option the node sends
     an error if high level is reached and on high high level.</p>

     <p>With reaching the high level there comes a warning
     and with the high high an error for work with the catch node</p>

     <p>Output: all queue information - use complete msg with debug</p>

</script>
